<?php
declare(strict_types=1);

/**
 * 企业认证服务中心
 */
namespace Service\UserService;
use Service\BaseService;
use App\Service\Code\Normal;
use App\Model\PssModel\Business;
class AccountEnterpriseService extends BaseService
{
    const AUTH_TYPE_ID_CARD = 1;
    const AUTH_TYPE_BUS_NO  = 2;
    
    /**
     * 证件类型枚举
     */
    public $types = 
    [
        [
            'key'    => self::AUTH_TYPE_ID_CARD,
            'value'  => '身份证'
        ],
        [
            'key'    => self::AUTH_TYPE_BUS_NO,
            'value'  => '营业执照'
        ],
    ];
    
    /**
     * 获取认证选项
     * @return unknown[]|string[]|array[]
     */
    public function accountEnterpriseAuthTypeOption()
    {
        return Normal::returnCode(Normal::SUC,"获取成功",$this->types);
    }
    
    /**
     * 企业认证
     * @param array $param
     */
    public function checkSaveUserPrise(array $param)
    {
        $binfo = $param['binfo'];
        $body  = $param['body'];
        
        if( empty($body['enterPriseName']) ) {
            return Normal::returnCode(Normal::FAIL,"企业名称不能为空!");
        }
        
        if( empty($body['enterPriseType']) ) {
            return Normal::returnCode(Normal::FAIL,"证件类型不能为空!");
        }
        
        if( empty($body['enterPriseNo']) ) {
            return Normal::returnCode(Normal::FAIL,"企业营业执照编号不能为空!");
        }
        
        $enterPriseNo   = $body['enterPriseNo'];
        $enterPriseType = $body['enterPriseType'];
        $enterPriseName = $body['enterPriseName'];
        
        $types = array_column($this->types,null,'key');
        if( empty($types[$enterPriseType]) ) {
            return Normal::returnCode(Normal::FAIL,"证件类型不正确!");
        }
        
        //验证证书编号数据
        if( $enterPriseType == self::AUTH_TYPE_ID_CARD ) {
            if( ! checkIdCard($enterPriseNo) ) {
                return Normal::returnCode(Normal::FAIL,"身份证号不规范，请核实之后重新输入!");
            }
        } else {
            if( ! checkOrgCode($enterPriseNo) ) {
                return Normal::returnCode(Normal::FAIL,"企业营业执照编号不规范，请核实之后重新输入!");
            }
        }
        
        //验证企业名称
        if( checkIsNoVolidString($enterPriseName) ) {
            return Normal::returnCode(Normal::FAIL,"企业名称不规范，不能包含特殊字符，请核实之后重新输入!");
        }
        
        Business::instance()->where('id',$binfo['bid'])->update([
            'enterprise_name'       => $enterPriseName,
            'enterprise_auth_type'  => $enterPriseType,
            'enterprise_auth_no'    => $enterPriseNo,
            'enterprise_auth_time'  => time(),
        ]);
        return Normal::returnCode(Normal::SUC,"成功");
    }
}